﻿using JTActiveSafety.Gateway.Abstractions;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;

namespace JTActiveSafety.Gateway.InMemoryMQ
{/// <summary>
/// 
/// </summary>
    public class JTActiveSafetyMsgProducer : IJTActiveSafetyMsgProducer, IDisposable
    {/// <summary>
    /// 
    /// </summary>
        public string TopicName { get; } = "JTActiveSafetyPackage";

        private readonly JTActiveSafetyMsgChannel _channel;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="channel"></param>
        public JTActiveSafetyMsgProducer(JTActiveSafetyMsgChannel channel)
        {
            _channel = channel;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sim"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public async ValueTask ProduceAsync(string sim, byte[] data)
        {
            await _channel.Input.WriteAsync((sim, data));
        }

        #region IDisposable

        private bool disposedValue;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)
                }

                // TODO: 释放未托管的资源(未托管的对象)并替代终结器
                // TODO: 将大型字段设置为 null
                disposedValue = true;
            }
        }

        // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
        // ~JTActiveSafetyMsgProducer()
        // {
        //     // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
        //     Dispose(disposing: false);
        // }
        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        } 
        #endregion
    }
}
